<template>
	<view class="home-wrap" :style="{'background':' linear-gradient(359deg, #F5F4F9 0%, #E7EEFE 100%); height: 100vh;'}">
		<shopro-navbar :isBack="false" :background="{'background':' transparent'}">
			<text slot="content" class="u-flex u-flex-center u-flex-items-center" style="color: #000; width: 100%; text-align: center;padding-left: 20rpx;height: 100%;">低空经济</text>
		</shopro-navbar>
		<view class="u-margin-20">
			<image :src="HTTP_IMG_URL() + '/static/images/banner.png'" class="banner-img" mode="aspectFill" />
			<CountdownCard :configInfo="configInfo" :examType="examType" />
			<FeatureEntry @entry-click="onFeatureEntry" />
			<view class="course-section">
				<view class="section-header">
					<text class="section-title">{{examType}}</text>
					<text class="more-btn" @click="onMoreClick()">更多 ></text>
				</view>
				<scroll-view class="course-list" scroll-x v-if="coursesList.length > 0">
					<CourseCardLine
						v-for="(item, idx) in coursesList"
						:key="idx"
						:courseId="item.id"
						:img="item.image"
						:time="item.createtime"
						:title="item.name"
						:buttonType="item.buttonType"
						@goStudy="goStudy"
						@goBuy="goBuy(item)"
						@imgClick="imgClick"
					/>
				</scroll-view>
				<view style="padding: 30px; text-align: center;" v-else>
					暂无课程数据
				</view>
				<view class="section-actions">
					<view class="action-btn u-flex u-flex-center u-flex-items-center" @click="actionClick('chapter')">
						<image :src="HTTP_IMG_URL()+'/static/images/tishiicon.png'"></image>
						<view>章节练习</view>
					</view>
					<view class="action-btn u-flex u-flex-center u-flex-items-center" @click="actionClick('mock')">
						<image :src="HTTP_IMG_URL()+'/static/images/tishiicon.png'"></image>
						<view>模拟试卷</view>
					</view>
					<view class="action-btn u-flex u-flex-center u-flex-items-center" @click="actionClick('wrong')">
						<image :src="HTTP_IMG_URL()+'/static/images/tishiicon.png'"></image>
						<view>错题本</view>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
import { mapGetters } from "vuex";
import CountdownCard from '@/components/CountdownCard.vue'
import FeatureEntry from '@/components/FeatureEntry.vue'
import CourseCardLine from "@/components/CourseCardLine.vue";
import recordApi from '@/api/record.js'
import { timestampToTime } from '@/utils/utils.js'
export default {
	components: {
		CourseCardLine,
		CountdownCard,
		FeatureEntry
	},
	data() {
		return {
			coursesList: [],
			configInfo: {},
			examType: 'CAAC'
		}
	},
	computed: mapGetters(['isLogin', 'uid', 'userInfo']),
	onLoad() {
		this.getCourseList();
		this.getSystemConfig();
	},
	onShow() {
		const examType = wx.getStorageSync('examType');
		console.log(examType, 'uni.getStorageInfoSync');
		if(examType){
			this.examType = examType;
		}
		this.getCourseList();
	},
	methods: {
		HTTP_IMG_URL() {
			return this.$imgUrl;
		},
		getCourseList () {
			let type = '';
			if(this.examType === 'CAAC') {
				type = '1';
			} else if(this.examType == 'AOPA'){
				type = '2';
			} else {
				type = '3';
			}
			recordApi.getInfoList({
				type: type
			}).then((res) => {
				if (res.response.data.code == 1) {
					this.coursesList = res.response.data.data.data;
					console.log(res.response.data.data.data, 'getCourseList');
				}
			})
		},
		getSystemConfig () {
			recordApi.getSystemConfig().then((res) => {
				if (res.response.data.code == 1) {
					this.configInfo = res.response.data.data;
					console.log(res.response.data.data, 'getSystemConfig');
				}
			})
		},
		// 功能入口点击
		onFeatureEntry(item) {
			if (item.key === 'video'){
				uni.switchTab({
					url: item.url,
				})
			} else{
				uni.navigateTo({
					url: item.url,
				})
			}
		},
		// 课程区更多
		onMoreClick() {
			uni.switchTab({
				url: '/pages/course/course',
			})
		},
		imgClick(id){
			uni.navigateTo( {
				url:'/pages/course/detail?courseId=' + id
			})
		},
		goBuy(item) {
			console.log(item, 'iiittteee');
			uni.navigateTo({
				url:'/pages/pay/pay?courseId=' + item.id
			})
		},
		goStudy(id) {
			// 去学习逻辑
			uni.navigateTo({
				url:'/pages/record/courseRecord?courseId=' + id
			})
		},
		actionClick(type){
			if(type == 'chapter'){
				uni.setStorageSync('questionnaireType', type);
				uni.switchTab({
					url:'/pages/questionnaire/questionnaire?type='+type
				})
			} else if(type == 'mock'){
				console.log("type=======",type)
				uni.setStorageSync('questionnaireType', type);
				uni.switchTab({
					url:'/pages/questionnaire/questionnaire?type='+type
				})
			} else if (type == 'wrong'){
				// 去错题本
				uni.navigateTo({
					url:'/pages/questionnaire/wrongQuestionnaire',
				})
			}
			
		}
	}
}
</script>

<style scope lang="scss">
	.banner-img {
		width: 100%;
		height: 300rpx;
		border-radius: 20rpx;
		margin: 0rpx 0 0;
		border-radius: 40rpx;
	}
	.course-section {
		background: url() no-repeat center;
		background-size: 100% 100%;
		box-shadow: 0px 2px 4px 0px rgba(66,120,253,0.08);
		border-radius: 20rpx;
		padding: 24rpx;
		// margin: 24rpx 0;
		.section-header {
			display: flex;
			justify-content: space-between;
			align-items: center;
			margin-bottom: 16rpx;
		}
		
		.section-title {
			font-size: 32rpx;
			font-weight: bold;
		}
		
		.more-btn {
			color: #fff;
			font-size: 24rpx;
			margin-bottom: 20rpx;
			padding-right: 10px;
		}
		
		.course-list {
			display: flex;
			flex-direction: row;
			overflow-x: scroll;
			white-space: nowrap;
			width: 100%;
		}
		
		.section-actions {
			display: flex;
			justify-content: space-between;
			margin-top: 20rpx;
		}
		
		.action-btn {
			flex: 1;
			margin: 0 8rpx;
			background: #e3eefe;
			color: #2563eb;
			border-radius: 12rpx;
			font-size: 24rpx;
			padding: 12rpx 0;
			image{
				width: 26rpx;
				height: 26rpx;
				margin-right: 8rpx;
			}
		}
	}

</style>